package com.qf.security;

import com.qf.api.entity.SysUserEntity;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/21 21:02
 * @Description:
 **/
@Data
public class SysLoginUser implements UserDetails {

    private SysUserEntity user;

    private List<GrantedAuthority> authorities;

    /**
     * 获取用户所具有的权限perms
     *
     * @return 返回用户所具有的权限列表，如果没有权限则返回null
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    /**
     * 判断账户是否非过期
     *
     * @return 如果账户过期返回false，否则返回true
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 判断账户是否被锁定（逻辑删除）
     *
     * @return 如果账户被锁定返回true，否则返回false
     */
    @Override
    public boolean isAccountNonLocked() {
        return !user.getDeleted();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 判断当前对象是否启用
     *
     * @return 如果当前对象启用，返回true；否则返回false
     */
    @Override
    public boolean isEnabled() {
        return user.getStatus();
    }
}
